import * as apis from 'services/address'

const types = {
  GET_ADDRESS_STRUCT_SUCCESS: 'GET_ADDRESS_STRUCT_SUCCESS',
  GET_ADDRESS_STRUCT_FAILURE: ' GET_ADDRESS_STRUCT_FAILURE',
  GET_ADDRESS_BY_ID_SUCCESS: 'GET_ADDRESS_BY_ID_SUCCESS',
  GET_ADDRESS_BY_ID_FAILURE: 'GET_ADDRESS_BY_ID_FAILURE',
  DELETE_ADDRESS_BY_ID_SUCCESS: 'DELETE_ADDRESS_BY_ID_SUCCESS',
  DELETE_ADDRESS_BY_ID_FAILURE: 'DELETE_ADDRESS_BY_ID_FAILURE',
  SAVE_ADDRESS_PENDING: 'SAVE_ADDRESS_PENDING',
  SAVE_ADDRESS_SUCCESS: 'SAVE_ADDRESS_SUCCESS',
  SAVE_ADDRESS_FAILURE: 'SAVE_ADDRESS_FAILURE',
  GET_ADDRESS_LIST_SUCCESS: 'GET_ADDRESS_LIST_SUCCESS',
  SELECT_ADDRESS: 'SELECT_ADDRESS',
}

const state = {
  addressStruct: {},
  loading: false,
  addressList: [],
  selected: {},
}

const actions = {
  getAddressList({ commit }) {
    return apis.getAddressList()
      .then((res) => {
        if (res.code === 1000) {
          commit(types.GET_ADDRESS_LIST_SUCCESS, res.data)
        }
        return res
      })
  },
  getAddressStruct({ commit }) {
    return apis.getAddressStruct()
      .then((res) => {
        if (res.code === 1000) {
          commit(types.GET_ADDRESS_STRUCT_SUCCESS, res.info)
        }
        return res
      })
  },
  getAddressById({ commit }, addressId) {
    return apis.getAddressById(addressId)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.GET_ADDRESS_BY_ID_SUCCESS, res.data)
        }
        return res
      })
  },
  deleteAddressById({ commit }, addressId) {
    commit(types.SAVE_ADDRESS_PENDING)

    return apis.deleteAddressById(addressId)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.DELETE_ADDRESS_BY_ID_SUCCESS, addressId)
        } else {
          commit(types.DELETE_ADDRESS_BY_ID_FAILURE)
        }
        return res
      })
  },
  saveAddress({ commit }, payload) {
    commit(types.SAVE_ADDRESS_PENDING)

    return apis.saveAddress(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.SAVE_ADDRESS_SUCCESS)
        } else {
          commit(types.SAVE_ADDRESS_FAILURE)
        }
        return res
      })
  },
  selectAddress({ commit }, addressId) {
    commit(types.SELECT_ADDRESS, addressId)
  },
}

const mutations = {
  [types.GET_ADDRESS_STRUCT_SUCCESS](state, info) {
    state.loading = false
    state.addressStruct = info
  },
  [types.SAVE_ADDRESS_PENDING](state) {
    state.loading = true
  },
  [types.SAVE_ADDRESS_SUCCESS](state) {
    state.loading = false
  },
  [types.SAVE_ADDRESS_FAILURE](state) {
    state.loading = false
  },
  [types.GET_ADDRESS_BY_ID_SUCCESS](state, data) {
    state.addressStruct = data.data
  },
  [types.DELETE_ADDRESS_BY_ID_SUCCESS](state, addressId) {
    state.addressList = state.addressList.filter(item => item.addressId !== addressId)
    state.loading = false
  },
  [types.DELETE_ADDRESS_BY_ID_FAILURE](state) {
    state.loading = false
  },
  [types.GET_ADDRESS_LIST_SUCCESS](state, data) {
    state.addressList = data
    state.selected = state.addressList.find(item => item.primary === 1) || {}
  },
  [types.SELECT_ADDRESS](state, addressId) {
    state.selected = state.addressList.find(item => item.addressId === addressId) || {}
  },
}

export default {
  state,
  actions,
  mutations,
}
